package cn.com.graPro.service;

import cn.com.graPro.utils.FileUtil;
import cn.com.graPro.utils.Base64Util;
import cn.com.graPro.utils.HttpUtil;

import java.net.URLEncoder;


/**
 * 用户头像审核API.
 */
public class FaceAuditService {

    /**
     * 重要提示代码中所需工具类
     * FileUtil,Base64Util,HttpUtil,GsonUtils请从
     * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
     * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
     * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
     * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
     * 下载
     */
    public static void main(String[] args) {
        // 用户头像审核 url
        String faceAuditUrl = "https://aip.baidubce.com/rest/2.0/solution/v1/face_audit";
        // 请求参数
        String configId = "1";
        String filePath = "#####本地文件路径#####";
        try {
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            String params = "configId=" + configId + "&" + URLEncoder.encode("images", "UTF-8") + "=" + URLEncoder
                    .encode(imgStr, "UTF-8");
            String accessToken = "#####调用鉴权接口获取的token#####";
            String result = HttpUtil.post(faceAuditUrl, accessToken, params);
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}